package com.lin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lin.entity.CarUserBill;
import com.lin.entity.CompInformation;
import com.lin.mapper.CarUserBillMapper;
import com.lin.mapper.CompInformationMapper;
import com.lin.service.CompInformationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lin.vo.DataVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 林伟鸿
 * @since 2021-11-30
 */
@Service
public class CompInformationServiceImpl extends ServiceImpl<CompInformationMapper, CompInformation> implements CompInformationService {

    @Autowired
    CompInformationMapper compInformationMapper;

    @Autowired
    CarUserBillMapper carUserBillMapper;

    DataVO<CompInformation> dataVO;
    QueryWrapper<CompInformation> queryWrapper;

    @Override
    public DataVO<CompInformation> getUserCompInfo(Integer page, Integer limit, Integer userId) {
        queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        IPage<CompInformation> iPage = new Page<>(page, limit);
        IPage<CompInformation> informationIPage = compInformationMapper.selectPage(iPage, queryWrapper);
        dataVO = new DataVO<>();
        dataVO.setMsg("");
        dataVO.setCode(0);
        dataVO.setCount(informationIPage.getTotal());
        dataVO.setData(informationIPage.getRecords());
        return dataVO;


    }

    @Override
    public int compensate(String orderSerial, String userId) {
        queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_serial", orderSerial).eq("user_id", userId);
        CompInformation compInformation = compInformationMapper.selectOne(queryWrapper);
        compInformation.setState("已赔付");
        return carUserBillMapper.deleteById(compInformation.getOrderSerial());
    }
}
